<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- tutorial.qdoc -->
  <title>QML Tutorial 1 - Basic Types | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td >QML 教程 1 - 基本类型</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qml-tutorial.html" />
  <link rel="next" href="qml-tutorial2.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-tutorial.html">QML 教程</a>
<span class="naviSeparator">  &#9702;  
<a class="nextPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-tutorial2.html">QML 教程 2 - QML 组件</a></span>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-tutorial1.html#walkthrough">演练</a></li>
<li class="level2"><a href="#import">导入</a></li>
<li class="level2"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-tutorial1.html#rectangle-type">矩形类型</a></li>
<li class="level2"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-tutorial1.html#text-type">文本类型</a></li>
<li class="level2"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-tutorial1.html#viewing-the-example">查看示例</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML 教程 1 - 基本类型</h1>
<span class="subtitle"></span>
<!-- $$$qml-tutorial1.html-description -->
<div class="descr"> <a name="details"></a>
<p>第一个程序是一个非常简单的“Hello world”示例，介绍了一些基本的 QML 概念。下图是这个程序的截图。</p>
<p class="centerAlign"><img src="images/declarative-tutorial1.png" alt="" /></p><p>这是应用程序的 QML 代码：</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">page</span>
      <span class="name">width</span>: <span class="number">320</span>; <span class="name">height</span>: <span class="number">480</span>
      <span class="name">color</span>: <span class="string">&quot;lightgray&quot;</span>

      <span class="type"><a href="qml-qtquick-text.html">Text</a></span> {
          <span class="name">id</span>: <span class="name">helloText</span>
          <span class="name">text</span>: <span class="string">&quot;Hello world!&quot;</span>
          <span class="name">y</span>: <span class="number">30</span>
          <span class="name">anchors</span>.horizontalCenter: <span class="name">page</span>.<span class="name">horizontalCenter</span>
          <span class="name">font</span>.pointSize: <span class="number">24</span>; <span class="name">font</span>.bold: <span class="number">true</span>
      }
  }

</pre>
<a name="walkthrough"></a>
<h2 id="walkthrough">演练</h2>
<h3 >导入</h3>
<p>First, we need to import the types that we need for this example. Most QML files will import the built-in QML types (like <a href="qml-qtquick-rectangle.html">Rectangle</a>, <a href="qml-qtquick-image.html">Image</a>, ..&#x2e;) that come with Qt, using:</p>
<pre class="qml">

  import QtQuick 2.0

</pre>
<a name="rectangle-type"></a>
<h3 >矩形类型</h3>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">page</span>
      <span class="name">width</span>: <span class="number">320</span>; <span class="name">height</span>: <span class="number">480</span>
      <span class="name">color</span>: <span class="string">&quot;lightgray&quot;</span>

</pre>
<p>我们声明一个<a href="qml-qtquick-rectangle.html">Rectangle</a>类型的根对象。它是可以用来在QML中创建应用程序的基本构建块之一。我们给它一个id，以便以后可以引用它。在本例中，我们称之为“page”。我们还设置了<code>width</code>,<code>height</code>和<code>color</code>属性。<a href="qml-qtquick-rectangle.html">Rectangle</a>类型包含许多其他属性(例如x和y)，但这些属性保留为默认值。</p>
<a name="text-type"></a>
<h3 >文本类型</h3>
<pre class="qml">

      <span class="type"><a href="qml-qtquick-text.html">Text</a></span> {
          <span class="name">id</span>: <span class="name">helloText</span>
          <span class="name">text</span>: <span class="string">&quot;Hello world!&quot;</span>
          <span class="name">y</span>: <span class="number">30</span>
          <span class="name">anchors</span>.horizontalCenter: <span class="name">page</span>.<span class="name">horizontalCenter</span>
          <span class="name">font</span>.pointSize: <span class="number">24</span>; <span class="name">font</span>.bold: <span class="number">true</span>
      }

</pre>
<p>我们添加一个<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-text.html">Text</a>类型作为根 Rectangle 类型的子类型，它显示文本“Hello world！”。</p>
<p>该<code>y</code>属性用于将文本垂直定位在距其父级顶部 30 像素处。</p>
<p><code>anchors.horizontalCenter</code>属性是指类型的水平中心。在这种情况下，我们指定我们的文本类型应该在<i>页面</i>元素中水平居中（请参阅<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-positioning-anchors.html">基于锚的布局</a>）。</p>
<p><code>font.pointSize</code>和<code>font.bold</code>属性与字体相关，并使用点表示法。</p>
<a name="viewing-the-example"></a>
<h3 >查看示例</h3>
<p>要查看您所创建的内容，运行<a href="../qtdoc/qtquick-qmlscene.html">qmlscene</a>工具(位于<code>bin</code> 
目录中)，将文件名作为第一个参数。例如，要从安装位置运行提供的教程1的完整示例，您可以输入:</p>
<pre class="cpp">

  qmlscene tutorials<span class="operator">/</span>helloworld<span class="operator">/</span>tutorial1<span class="operator">.</span>qml

</pre>
</div>
<!-- @@@qml-tutorial1.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-tutorial.html">QML 教程</a>
<span class="naviSeparator">  &#9702;  
<a class="nextPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-tutorial2.html">QML 教程 2 - QML 组件</a></span>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
